<?php

namespace app\index\controller;

use app\common\model\Behavior;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use think\Controller;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Reader\Xls;
use PhpOffice\PhpSpreadsheet\Reader\Csv;

class Test extends Controller
{
    public function index()
    {
        $list = Behavior::hasWhere('category', ['id' => 5])->with('category')->select();

        return json($list);


        Order::where(function ($query) use ($mobile, $brand_id, $product_id) {
                if ($mobile) {
                    $query->hasWhere('user', ['mobile' => '1111']);
                }
                if ($brand_id) {
                    $query->where('brand_id', $brand_id);
                }
            })
            ->select();
    }

    function randomFloat($min = 0, $max = 1)
    {
        $num = $min + mt_rand() / mt_getrandmax() * ($max - $min);
        return sprintf("%.2f", $num);  //控制小数后几位

    }

    public function export()
    {
        $data = [];
        for ($i = 3; $i <= 8; $i++) {

            $day = "2020-0$i-01";
            $days = date('t', strtotime($day));

            for ($j = 0; $j < $days; $j++) {

                $wechat = $this->randomFloat(2400, 3500);
                $xianjin = $this->randomFloat(500, 1000);

                $total = $this->randomFloat(7000, 10000);
                $alipay = $total - $wechat - $xianjin;

                $data[] = [
                    $day,
                    $wechat,
                    $alipay,
                    $xianjin,
                    $total
                ];

                $day = date("Y-m-d", strtotime($day . "+1 day"));
            }
        }

        $title = ['日期', '微信', '支付宝', '现金', '合计'];

        $spreadsheet = new Spreadsheet();
        $worksheet = $spreadsheet->getActiveSheet();

        //设置工作表标题名称
        $worksheet->setTitle('工作表格1');

        //表头
        //设置单元格内容
        foreach ($title as $key => $value) {
            $worksheet->setCellValueByColumnAndRow($key + 1, 1, $value);
        }

        $row = 2; //从第二行开始
        foreach ($data as $item) {
            $column = 1;

            foreach ($item as $value) {
                $worksheet->setCellValueByColumnAndRow($column, $row, $value);
                $column++;
            }
            $row++;
        }


        $fileName = '学生信息';
        $fileType = 'Xlsx';

        //1.下载到服务器
        //$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
        //$writer->save($fileName.'.'.$fileType);

        //2.输出到浏览器
        $writer = IOFactory::createWriter($spreadsheet, 'Xlsx'); //按照指定格式生成Excel文件
        $this->excelBrowserExport($fileName, 'Xlsx');
        $writer->save('php://output');
    }

    function excelBrowserExport($fileName, $fileType)
    {

        //文件名称校验
        if (!$fileName) {
            trigger_error('文件名不能为空', E_USER_ERROR);
        }

        //Excel文件类型校验
        $type = ['Excel2007', 'Xlsx', 'Excel5', 'xls'];
        if (!in_array($fileType, $type)) {
            trigger_error('未知文件类型', E_USER_ERROR);
        }

        if ($fileType == 'Excel2007' || $fileType == 'Xlsx') {
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
            header('Content-Disposition: attachment;filename="' . $fileName . '.xlsx"');
            header('Cache-Control: max-age=0');
        } else { //Excel5
            header('Content-Type: application/vnd.ms-excel');
            header('Content-Disposition: attachment;filename="' . $fileName . '.xls"');
            header('Cache-Control: max-age=0');
        }
    }

}